package com.example.server.mapper;

import com.example.server.entity.RecentPlayedScript;
import com.example.server.entity.Role;
import com.example.server.entity.Scene;
import com.example.server.entity.Script;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ScriptMapper {

    //添加剧本
    @Insert("insert into script(title,author,content,tags)"
            + "values(#{title},#{author},#{content},#{tags})")
    void insertScript(@Param("title") String title,
                      @Param("author") String author,
                      @Param("content") String content,
                      @Param("tags") String tags);

    //根据剧本名拿到剧本
    @Select("select * from script where title=#{title}")
    Script selectScriptByTitle(@Param("title") String title);

    // 分页查找所有剧本
    @Select("SELECT * FROM script ORDER BY id ")
    List<Script> selectAllScripts();

    //根据主键查剧本
    @Select("select * from script where id = #{id}")
    Script selectScriptById(@Param("id") Integer id);

    // 上传剧本图片路径
    @Update("UPDATE Script SET image= #{image} WHERE id = #{id} ")
    void uploadScriptImage(@Param("id") Integer id, @Param("image") String image);

    //查询用户最近游玩剧本
    @Select("select s.* ,us.id as conversation_id ,us.play_time as play_time from script s " +
            "join conversation us on s.id = us.script_id " +
            "where us.user_id = #{id} " +
            "order by us.play_time desc " +
            "limit 10")
    List<RecentPlayedScript> selectRecentPlayed(@Param("id") Integer id);

    //根据名称查剧本（模糊查）
    @Select("select * from script " +
            "where title like concat('%',#{title},'%') ")
    List<Script> selectScriptsByTitle(@Param("title") String title);

    //根据剧本id修改剧本 title，content，tags等字段
    @Update("UPDATE Script SET title = #{title}, " +
            " content = #{content}, tags = #{tags} " +
            "WHERE id = #{id}")
    void updateScript(@Param("id") Integer id,
                      @Param("title") String title,
                      @Param("content") String content,
                      @Param("tags") String tags);

    //查询某个作者（也就是用户名）上传的剧本
    @Select("SELECT * FROM script WHERE author = #{author}")
    List<Script> selectScriptsByAuthor(@Param("author") String author);

    //删除剧本
    @Delete("DELETE FROM script WHERE id = #{id}")
    void deleteScriptById(@Param("id") Integer id);

    // 更新剧本初始角色
    @Update("UPDATE role SET script_id = #{scriptId}," +
            " name = #{name}, description = #{description}," +
            " traits = #{traits} WHERE id = #{id}")
    void updateRole(@Param("id") Integer id,
                    @Param("scriptId") Integer scriptId,
                    @Param("name") String name,
                    @Param("description") String description,
                    @Param("traits") String traits);

    // 更新剧本初始场景
    @Update("UPDATE scene SET  " +
            " description = #{description} WHERE id = #{id}")
    void updateScene(@Param("id") Integer id,
                     @Param("description") String description);

    // 创建剧本时，创建初始角色
    @Insert("INSERT INTO role(script_id, name, description, traits)" +
            " VALUES(#{scriptId}, #{name}," +
            " #{description}, #{traits})")
    void insertRole(@Param("scriptId") Integer scriptId,
                    @Param("name") String name,
                    @Param("description") String description,
                    @Param("traits") String traits);

    // 创建剧本时，指定初始场景
    @Insert("INSERT INTO scene(script_id, description)" +
            " VALUES(#{scriptId}, #{description})")
    void insertScene(@Param("scriptId") Integer scriptId,
                     @Param("description") String description);

    // 删除角色
    @Delete("DELETE FROM role WHERE id = #{id}")
    void deleteRoleById(@Param("id") Integer id);

    // 删除场景
    @Delete("DELETE FROM scene WHERE id = #{id}")
    void deleteSceneById(@Param("id") Integer id);

    // Select a role by its ID
    @Select("SELECT * FROM role WHERE id = #{id}")
    Role selectRoleById(@Param("id") Integer id);

    // Select all roles for a specific script
    @Select("SELECT * FROM role WHERE script_id = #{scriptId}")
    List<Role> selectRolesByScriptId(@Param("scriptId") Integer scriptId);

    // 查某个初始化场景
    @Select("SELECT * FROM scene WHERE id = #{id}")
    Scene selectSceneById(@Param("id") Integer id);

    // 查一个剧本的全部初始化场景
    @Select("SELECT * FROM scene WHERE script_id = #{scriptId}")
    Scene selectScenesByScriptId(@Param("scriptId") Integer scriptId);
}
